﻿using System;
using System.Windows.Media;
using System.Globalization;

namespace AMing.Extension.WindowsPhone8
{
    public static class ExtensionString
    {
        /// <summary>
        /// 转成指定类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="val"></param>
        /// <param name="defaultval">转换失败的设置的值</param>
        /// <returns></returns>
        public static T To<T>(this string val, T defaultval = default(T)) where T : struct
        {
            try
            {
                if (string.IsNullOrWhiteSpace(val))
                {
                    return defaultval;
                }
                return (T)Convert.ChangeType(val, typeof(T), null);
            }
            catch (Exception)
            {
                return defaultval;
            }
        }

        /// <summary>
        /// 将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。
        /// </summary>
        /// <param name="val"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string ToFromat(this string val, params object[] args)
        {
            return string.Format(val, args);
        }

        /// <summary>
        /// 十六进制值转成颜色值
        /// </summary>
        /// <param name="val"></param>
        /// <param name="defaultval"></param>
        /// <returns></returns>
        public static Color ToColor(this string val, Color defaultval)
        {
            try
            {
                byte a = 255;
                byte r = 255;
                byte g = 255;
                byte b = 255;
                if (!string.IsNullOrWhiteSpace(val) && val.Substring(0, 1) == "#")
                {
                    val = val.Substring(1);
                    if (val.Length == 3)
                    {
                        string r_str = val.Substring(0, 1);
                        string g_str = val.Substring(1, 1);
                        string b_str = val.Substring(2, 1);

                        r = byte.Parse(r_str + r_str, NumberStyles.HexNumber);
                        g = byte.Parse(g_str + g_str, NumberStyles.HexNumber);
                        b = byte.Parse(b_str + b_str, NumberStyles.HexNumber);
                    }
                    else if (val.Length == 6)
                    {
                        string r_str = val.Substring(0, 2);
                        string g_str = val.Substring(2, 2);
                        string b_str = val.Substring(4, 2);

                        r = byte.Parse(r_str, NumberStyles.HexNumber);
                        g = byte.Parse(g_str, NumberStyles.HexNumber);
                        b = byte.Parse(b_str, NumberStyles.HexNumber);
                    }
                    else if (val.Length == 8)
                    {
                        string a_str = val.Substring(0, 2);
                        string r_str = val.Substring(2, 2);
                        string g_str = val.Substring(4, 2);
                        string b_str = val.Substring(6, 2);

                        a = byte.Parse(a_str, NumberStyles.HexNumber);
                        r = byte.Parse(r_str, NumberStyles.HexNumber);
                        g = byte.Parse(g_str, NumberStyles.HexNumber);
                        b = byte.Parse(b_str, NumberStyles.HexNumber);
                    }
                    else
                    {
                        return defaultval;
                    }

                    return Color.FromArgb(a, r, g, b);
                }

                return defaultval;
            }
            catch (Exception)
            {
                return defaultval;
            }
        }
    }
}
